package bb.lanxing.activity.sport;

import com.umeng.analytics.MobclickAgent;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.manager.RemoteServiceManager;
import bb.lanxing.chat.utils.IntentUtils;
import bb.lanxing.common.config.Constants;
import bb.lanxing.common.config.UmengEventConst;
import bb.lanxing.devicelist.XZDeviceListActivity;
import bb.lanxing.manager.RemoteSharedPreference;
import bb.lanxing.manager.SPConstant;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.view.BiciAlertDialogBuilder;

public class SportSettingActivity extends BaseActivity {
    private static final int REQUEST_CODE_TTS = 101;

    private Switch ttsSwitch;

    private SharedManager sharedManager = SharedManager.getInstance();
    private RemoteSharedPreference remoteSharedPreference = RemoteSharedPreference.getInstance();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sport_setting);

        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(v -> onBackPressed());

        ImageView help = findViewById(R.id.help);
        help.setOnClickListener(v -> IntentUtils.gotoWebBrowser(this.getActivity(), Constants.SPORT_HELP_URL, null));

        LinearLayout dashboardSettingLayout = findViewById(R.id.dashboardSettingLayout);
        dashboardSettingLayout.setOnClickListener(v -> {
            Intent intent = new Intent(this, PanelSettingActivity.class);
            intent.putExtra("sportType", getIntent().getIntExtra("sportType", 0));
            startActivity(intent);
        });

        Switch nightModeSwitch = findViewById(R.id.nightModeSwitch);
        nightModeSwitch.setChecked(sharedManager.isNightMode());
        nightModeSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            sharedManager.setNightMode(isChecked);
            sendMessage(R.id.msg_sport_setting_day_night_changed, isChecked);
            MobclickAgent.onEventValue(App.getContext(), isChecked ? UmengEventConst.V30_SETTING_NIGHT_OPEN : UmengEventConst.V30_SETTING_NIGHT_CLOSE, null, 1);
        });

        Switch bigFontModeSwitch = findViewById(R.id.bigFontModeSwitch);
        bigFontModeSwitch.setChecked(sharedManager.isBigFontMode());
        bigFontModeSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            sharedManager.setBigFontMode(isChecked);
            sendMessage(R.id.msg_sport_setting_big_font_changed, isChecked);
            MobclickAgent.onEventValue(App.getContext(), isChecked ? UmengEventConst.V30_SETTING_BIG_FONT_OPEN : UmengEventConst.V30_SETTING_BIG_FONT_CLOSE, null, 1);
        });

        Switch powerSavingModeSwitch = findViewById(R.id.powerSavingModeSwitch);
        powerSavingModeSwitch.setChecked(remoteSharedPreference.getBoolean(SPConstant.KEY_BATTERY_SAVING_MODE, false));
        powerSavingModeSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (RemoteServiceManager.getInstance().isSporting()) {
                toast(R.string.sport_setting_toast_sport_has_started);
                return;
            }
            remoteSharedPreference.setValue(SPConstant.KEY_BATTERY_SAVING_MODE, isChecked);
            if (isChecked && !sharedManager.isPowerSavingModeToast()) {
                new BiciAlertDialogBuilder(this).setTitle(R.string.sport_setting_battery_dialog_title)
                        .setMessage(R.string.sport_setting_battery_dialog_content).setPositiveButton(R.string.dialog_btn_known, null)
                        .setNegativeButton(R.string.dialog_do_not_prompt, (dialog, which) -> sharedManager.setPowerSavingModeToast(true)).show();
            }
            MobclickAgent.onEventValue(App.getContext(), isChecked ? UmengEventConst.V30_SETTING_POWERSAVING_OPEN : UmengEventConst.V30_SETTING_POWERSAVING_CLOSE, null, 1);
        });

        Switch screenOnSwitch = findViewById(R.id.screenOnSwitch);
        screenOnSwitch.setChecked(sharedManager.getBoolean(SPConstant.KEY_DASHBOARD_LIGHT_ALWAYS, false));
        screenOnSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            sharedManager.setValue(SPConstant.KEY_DASHBOARD_LIGHT_ALWAYS, isChecked);
            sendMessage(R.id.msg_sport_setting_screen_on_changed, isChecked);
        });

        ttsSwitch = findViewById(R.id.ttsSwitch);
        ttsSwitch.setChecked(remoteSharedPreference.getTTSSoundEnable());
        ttsSwitch.setOnClickListener(v -> {
            startActivityForResult(new Intent(this, SettingTTsActivity.class), REQUEST_CODE_TTS);
            MobclickAgent.onEventValue(App.getContext(), UmengEventConst.V30_SETTING_VOICE, null, 1);
        });

        Switch locationShareSwitch = findViewById(R.id.locationShareSwitch);
        locationShareSwitch.setChecked(sharedManager.getBoolean(SPConstant.KEY_UPLOAD_LOCATION, false));
        locationShareSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            boolean isLocationShare = sharedManager.getBoolean(SPConstant.KEY_UPLOAD_LOCATION, false);
            if (isLocationShare) {
                App.getContext().showMessage(R.string.sport_toast_loc_share_cannot_close);
            } else {
                sharedManager.setValue(SPConstant.KEY_UPLOAD_LOCATION, isChecked);
                // 有问题
                if (!isLocationShare) {
                    App.getContext().uploadMyLocationIfNeed(SharedManager.getInstance().getCurLatLngWithMP(),
                            null, null, 0, false, null);
                }
            }
            MobclickAgent.onEventValue(App.getContext(), isLocationShare ? UmengEventConst.V30_SETTING_LOCATION_OPEN : UmengEventConst.V30_SETTING_LOCATION_CLOSE, null, 1);
        });

        LinearLayout smartDeviceLayout = findViewById(R.id.smartDeviceLayout);
        smartDeviceLayout.setOnClickListener(v -> {
            startActivity(new Intent(this, XZDeviceListActivity.class));
            MobclickAgent.onEventValue(App.getContext(), UmengEventConst.V30_SETTING_SMARTDEVICE, null, 1);
        });

        LinearLayout offlineMapLayout = findViewById(R.id.offlineMapLayout);
        offlineMapLayout.setOnClickListener(v -> {
            startActivity(new Intent(this, BDOfflineManageActivity.class));
            MobclickAgent.onEventValue(App.getContext(), UmengEventConst.V30_SETTING_MAP_OFFLINE, null, 1);
        });

        LinearLayout cadenceHelperLayout = findViewById(R.id.cadenceHelperLayout);
        cadenceHelperLayout.setOnClickListener(v -> {
            startActivity(new Intent(this, CadenceTrainingActivity.class));
            MobclickAgent.onEventValue(App.getContext(), UmengEventConst.V30_SETTING_RPM_HELP, null, 1);
        });

        LinearLayout settingMoreLayout = findViewById(R.id.settingMoreLayout);
        settingMoreLayout.setOnClickListener(v -> {
            startActivity(new Intent(this, SettingMoreActivity.class));
            MobclickAgent.onEventValue(App.getContext(), UmengEventConst.V30_SETTING_MORE, null, 1);
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1 && requestCode == REQUEST_CODE_TTS) {
            ttsSwitch.setChecked(remoteSharedPreference.getTTSSoundEnable());
        }
    }
}